<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="40px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="全部" clearable style="width: 240px">
          <el-option value="" label="全部"></el-option>
          <el-option v-for="item in statusType" :key="item.status" :label="item.title" :value="item.status" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:dict:add']">
          新增对照
        </el-button>

      </el-col>
      <el-col :span="1.5">
        <el-button  icon="el-icon-delete" type="danger" style="margin-right: 20px" size="mini" @click="handleDel">批量删除
        </el-button>
        <el-dropdown trigger="click">
          <el-button type="primary" size="mini">
            导出<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleExport('now')">导出当前</el-dropdown-item>
            <el-dropdown-item @click.native="handleExport('all')">导出全部</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
    </el-row>
    <el-table :row-class-name="rowStyle" border highlight-current-row v-loading="loading" :data="sampleTypeList"
      @selection-change="handleDelChange" height="calc(100vh - 250px)" :header-cell-style="{
        background: '#ebebeb'
      }">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <af-table-column label="编码" align="center" prop="code"  />
      <af-table-column label="名称" align="center" prop="name"  />
      <af-table-column label="前缀" align="center" prop="prefix" width="200" />
      <af-table-column label="备注" align="center" prop="remark" width="250" show-overflow-tooltip />
      <af-table-column label="状态" align="center" prop="status" width="150">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status == 1" active-color="#169bd5" inactive-color="#aaaaaa"
            @change="switchShowStatus(scope.row)" />
        </template>
      </af-table-column>


      <el-table-column label="操作"  align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dict:edit']">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <pagination   style="margin:0;padding: 0;" :total="total" :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog title="获取LRP数据" :visible.sync="lrpOpen" width="1000px" append-to-body >
      
          <el-form ref="lrpForm" :model="lrpForm" :rules="rules" label-width="" :inline="true">
            <el-form-item label="名称" prop="name">
              <el-input v-model="lrpForm.keyword" placeholder="请输入名称" />
            </el-form-item>
            <el-form-item>
              <el-button size="mini" style="margin-left: 20px; height: 36px;" type="primary" @click="getLrpList">获取LRP数据
        </el-button>
            </el-form-item>
          </el-form>
        

        
     
      <div style="display: flex;">
        <el-table ref="multipleTable" border v-loading="loading" :data="lrpList"
          @selection-change="handleSelectionChange" height="500">
          <el-table-column type="selection" width="55" align="center" :selectable="setDisabled" />
          <el-table-column label="序号" align="center" type="index" />
          <el-table-column label="编码" align="center" prop="codeNo" show-overflow-tooltip />
          <el-table-column label="名称" align="center" prop="name" show-overflow-tooltip width="180"/>
          <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
        </el-table>

        <div class="transferBtn" @click="choose">
          <img src="../../../assets/image/right.png" style="width: 50px;height: 50px;" alt="">
        </div>

        <el-table :row-class-name="tableRowClassName" border v-loading="loading" :data="sampleCopyList" height="500">
          <el-table-column label="序号" align="center" type="index" />
          <el-table-column label="编码" align="center" prop="code" show-overflow-tooltip />
          <el-table-column label="名称" align="center" prop="name" show-overflow-tooltip width="180"/>
          <el-table-column label="前缀" align="center" prop="prefix" show-overflow-tooltip />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button v-if="scope.row.codeNo" size="mini" type="text" icon="el-icon-delete"
                @click="handleDelete(scope.row)" v-hasPermi="['system:dict:edit']" style="color: #FA5555;">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>


      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="refer">提 交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="样本类型：编辑" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" :disabled="true" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" :disabled="true" />
        </el-form-item>
        <el-form-item label="前缀" prop="prefix">
          <el-input v-model="form.prefix" placeholder="请输入前缀"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
       
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { sampleType, sampleTypeUpdate, sampleTypeEdit, sampleTypeExport, sampleBatchComparison, lrpSampleList, sampleTypeDel } from "@/api/dictionary/systemDictionary/sampleType"
import { professional } from '@/api/dictionary/systemDictionary/professional'

export default {
  name: "sampleType",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      // 总条数
      total: 0,
      // 核酸类型数据
      sampleTypeList: [],
      sampleCopyList: [],
      submitList: [],
      lrpList: [],
      // 是否显示弹出层
      open: false,
      lrpOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        name: undefined,
        status: '',
      },
      statusType: [
        {
          title: '开启',
          status: 1
        },
        {
          title: '禁用',
          status: 0
        }
      ],
      // 表单参数
      form: {},
      //LRP数据
      lrpForm: {},
      // 表单校验
      rules: {

      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /** 查询样本类型列表 */
    getList () {
      this.loading = true
      sampleType(this.queryParams).then(response => {
        this.sampleTypeList = response.rows
        this.total = response.total
        this.loading = false
      }
      )
    },
    //** 获取lrp数据 */
    getLrpList () {
      lrpSampleList(this.lrpForm).then(response => {
        this.lrpList = response.data
        console.log(response)
      })
      sampleType().then(response => {
        this.sampleCopyList = response.rows      
      }
      )
    },
    // 取消按钮
    cancel () {
      this.sampleCopyList = []
      this.submitList = []
      this.lrpList = []
      this.lrpOpen = false
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.lrpOpen = true
    },
    // 多选框选中数据删除
    handleDelChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.submitList = []
      selection.map(item => {
        item.id = ''
        item.code = item.codeNo
        let dataFind = this.sampleCopyList.find(zhe => zhe.code === item.code)
        if (dataFind) {
          item.id = dataFind.id
          item.prefix = dataFind.prefix
        }
        this.submitList.push(item)
      })
    },
    //选中的数据
    choose () {
      for (let k = 0; k < this.sampleCopyList.length; k++) {
        let assignment = this.submitList.find(
          item => item.codeNo === this.sampleCopyList[k].code
        )
        if (assignment) {
          this.sampleCopyList[k] = assignment
          this.submitList.splice(
            this.submitList.findIndex(
              ge => ge.code === this.sampleCopyList[k].code
            ),
            1
          )
        }
      }
      this.sampleCopyList = [...this.sampleCopyList, ...this.submitList]
      this.clearSelection()
    },
    //清除勾选
    clearSelection () {
      this.$refs.multipleTable.clearSelection()
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      const id = row.id || this.ids
      sampleTypeEdit(id).then(response => {
        this.form = response.data
        this.open = true
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            sampleTypeUpdate(this.form).then(response => {
              this.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    //批量删除
    handleDel () {
      if (this.ids.length === 0) {
        this.msgError('请选择删除项')
      } else {
        this.$confirm('是否确认删除选中的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(res => {
          return sampleTypeDel(this.ids)
        }).then(() => {
          this.getList()
          this.msgSuccess("删除成功")
        })
      }
      console.log(this.ids)
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      this.$confirm('是否确认删除选中的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(res => {
        console.log(row)
        this.sampleCopyList.splice(
          this.sampleCopyList.findIndex(
            item => item.codeNo === row.codeNo
          ),
          1
        )
      })
    },
    // /** 导出按钮操作 */
    handleExport (e) {
      var different = ''
      var params = ''
      if (e === 'now') {
        different = '当前'
        params = this.queryParams //对应接口参数
      } else if (e === 'all') {
        different = '所有'
        params = null
      } else {
        return
      }
      let that = this
      let loading
      this.$confirm('是否确认导出' + different + '数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        loading = that.$loading({
          lock: true,
          text: '导出中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        sampleType(params).then((response) => {
          let mydata = [
            [
              '序号',
              '编码',
              '名称',
              '前缀',
              '备注',
              '状态',
            ],
            ...response.rows.map((item, index) => ([
              index + 1,
              item.code,
              item.name,
              item.prefix,
              item.remark,
              item.status === 0 ? '禁用' : '开启',
            ]))
          ]
          let requireMerge = []
          that.$util.createxlsx({
            mydata,
            fileName: '样本类型',
            dataList: response.rows,
            type: 'orderId',
            requireMerge,
            loading
          })
        })
      }).catch(function () { })
    },
    //切换状态
    switchShowStatus (row) {
      if (row.status === 1) {
        this.handleUnPublish(row)
      } else if (row.status === 0) {
        this.handlePublish(row)
      }
    },
    handlePublish (row) {
      this.$confirm('是否开启', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return sampleTypeUpdate({
          ...row,
          status: 1
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("开启成功")
      }).catch(function () {
      })
    },


    handleUnPublish (row) {
      this.$confirm('是否禁用', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return sampleTypeUpdate({
          ...row,
          status: 0
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("禁用成功")
      }).catch(function () {
      })
    },

    //选择数据后禁用选择框
    setDisabled (row, index) {
      let dataFind = this.sampleCopyList.find(item => item.code === row.codeNo)
      if (dataFind) {
        if ((row.codeNo + row.name + row.remark) === (dataFind.code + dataFind.name + dataFind.remark)) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    },
    //新增和数据修改改变颜色
    tableRowClassName ({ row, rowIndex }) {
      let dataFind = this.sampleCopyList.find(item => item.code === row.codeNo)
      if (dataFind) {
        if ((row.codeNo + row.name + row.remark) === (dataFind.code + dataFind.name + dataFind.remark)) {
          return 'success-row'
        } else {
          return ''
        }
      } else {
        return ''
      }
    },
    //提交新增对照
    refer () {
      let delLenth = this.sampleCopyList.filter(item => {
        if (!item.codeNo) {
          return true
        }
      })
      for (let i = 0; i < delLenth.length; i++) {
        this.sampleCopyList.splice(
          this.sampleCopyList.findIndex(
            item => {
              if (!item.codeNo) {
                return true
              } else {
                return false
              }
            }
          ),
          1
        )
      }
      if(!this.sampleCopyList.length){
        this.msgError("请获取lrp数据")
        return
      }
      sampleBatchComparison({ dictSampleTypeList: this.sampleCopyList }).then(response => {
        this.msgSuccess("提交成功")
        this.lrpOpen = false
        this.sampleCopyList = []
        this.submitList = []
        this.lrpList = []
        this.getList()
      })
    },
    rowStyle ({ row }) {
      var arr = this.ids
      for (let i = 0; i < arr.length; i++) {
        if (row.id === arr[i]) {
          return "rowStyle"
        }
      }
    },
  }
}
</script>
<style scoped>
.app-container {
  height: 85vh;
}

.transferBtn {
  padding: 0 10px 0 10px;
  cursor: pointer;
  /* margin-top: 50%; */
  display: flex;
  align-items: center;
}

::v-deep .success-row {
  background: rgba(47, 120, 236, 0.19) !important;
}

::v-deep .rowStyle {
  background-color: #a8d6fe !important;
}

::v-deep .el-table__body tr.current-row>td {

  background: #a8d6fe !important;
}


::v-deep .el-table__body tr:hover>td {
  background: #a8d6fe !important;
}
::v-deep .el-dialog__body{
  padding:0 20px !important
}
</style>
